var CurrentUser;
function getName() {
    var user = JSON.parse(localStorage.getItem("loggedinUser"));
    CurrentUser = user;   //global variable
    if (user) {
        console.log("Currently logged in:  " + user["name"]);
        document.getElementById("name-goes-here").innerText = user["name"];
    } else {
        console.log("No one is logged in");
    }
}
getName();

function populateHistory() {
    // find out who is logged in  
    var users = JSON.parse(localStorage.getItem("users"));
    let email = CurrentUser["email"];
    const user = users.find(u => u.email === email);

    // get information for that user who is logged in 
    if (user) {
        historyArray = user["appointments"];
        
        historyArray.forEach((hist, index) => {
            let historyCardTemplate = document.getElementById("postCardTemplate");
            let historyCardGroup = document.getElementById("history-goes-here");
            let onecard = historyCardTemplate.content.cloneNode(true);


            
            onecard.querySelector(".date").innerHTML = hist.time;
            onecard.querySelector(".count").innerHTML = hist.name;
            onecard.querySelector(".card-description").innerHTML = hist.telephone;
            onecard.querySelector('.deleteButton').onclick = () => deleteHistory(index);
            onecard.querySelector('.editButton').onclick = () => editHistory(index);
            historyCardGroup.appendChild(onecard);

        })
    }
    else {
        console.log("No user found");
    }

}
populateHistory();